package com.lantosec.hmrp.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.modules.system.entity.SysArea;
import com.lantosec.hmrp.modules.system.entity.SysPermission;
import com.lantosec.hmrp.modules.system.mapper.SysPermissionMapper;
import com.lantosec.hmrp.modules.system.service.ISysPermissionService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统权限表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-02-15
 */
@Service
@Primary
@Log4j2
@SuppressWarnings("all")
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Override
    public Page<SysPermission> queryPermissionById(String permissionId, PageVO<SysPermission>page) {
        PageVO<SysPermission> param = baseMapper.selectPage(page, new QueryWrapper<SysPermission>().
                eq(String.valueOf(SysPermission.ColName.parent_id), permissionId));
        return param.setRecords(param.getRecords());
    }

    @Override
    public Integer queryOnlyPermissionName(String permissionName) {
        Integer count = baseMapper.selectCount(
                new QueryWrapper<SysPermission>()
                    .select()
                    .eq(StringUtils.isNotEmpty(permissionName), String.valueOf(SysPermission.ColName.permission_name), permissionName));
        return count;
    }

    @Override
    public void changeState(String permissionId, String state) {
        baseMapper.changeState(permissionId,state);
    }

    @Override
    public List<SysPermission> queryTopPermissionList() {
         return this.list(new QueryWrapper<SysPermission>().isNull(String.valueOf(SysPermission.ColName.parent_id))
                 .or()
                 .eq((SysPermission.ColName.parent_id.toString()),""));
    }

    @Override
    public String queryMaxId(String parentId) {
        String permissionId = null;
        if(StringUtils.isNotEmpty(permissionId)){
            permissionId = baseMapper.queryMaxIdByParentId(parentId);
            return StringUtils.isEmpty(permissionId) ? parentId + "001" :permissionId;
        }else {
            permissionId = baseMapper.queryMaxId();
            return StringUtils.isEmpty(permissionId) ? "001" : permissionId;
        }
    }

    @Override
    public List<TreeNodeVo> getPermissionTree() {
        return permissionTree(null);
    }

    private List<TreeNodeVo> permissionTree(String permissionId) {
        List<TreeNodeVo> list = permissionList(permissionId);
        List tree = new ArrayList<TreeNodeVo>();
        if (list != null && list.size() > 0) {
            list.forEach(node -> {
                if (!node.isLeaf()) {
                    List<TreeNodeVo> children = permissionTree(node.getKey());
                    node.setChildren(children);
                }
                tree.add(node);
            });
        }
        return tree;
    }

    private List<TreeNodeVo> permissionList(String permissionId) {
        List<SysPermission> list = getPermissionById(permissionId);
        if (list != null && list.size() > 0) {
            List<TreeNodeVo> nodes = new ArrayList<TreeNodeVo>();
            list.forEach( result -> {
                nodes.add(new TreeNodeVo( result.getPermissionId(),result.getPermissionShowName()));
            });
            return nodes;
        }
        return new ArrayList<TreeNodeVo>();
    }

    private List<SysPermission> getPermissionById(String permissionId) {
        return baseMapper.getPermissionById(permissionId);
    }
}
